/*******************************
* Students Construct Center 119			*
* C++ micro AVR Atmega 8/16 template	*
* 2012, Omsk State University			*
*****************************************/

/*******************************
* Name of project:
* Autors:
* Date:
********************************/

//define frequency here
#define F_CPU 12000000UL 

// includes
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <string.h>
#include <stdlib.h>

// define UART CONSTANT
#define BAUDRATE 19200 // speed of uart
#define UART_CALC_BAUDRATE(baudRate) ((uint32_t)((F_CPU) + ((uint32_t)baudRate * 8UL)) / ((uint32_t)(baudRate) * 16UL) - 1)
#define UART_BAUD_HIGH	UBRRH
#define UART_BAUD_LOW	UBRRL
#define UART_STATUS	UCSRA
#define UART_TXREADY	UDRE
#define UART_RXREADY	RXC
#define UART_DOUBLE	U2X
#define UART_CTRL	UCSRB
#define UART_CTRL_DATA	((1<<TXEN) | (1<<RXEN))
#define UART_CTRL2	UCSRC
#define UART_CTRL2_DATA	((1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0))
#define UART_DATA	UDR

// define ports here
#define SEN		(1 << 7)
#define SCK		(1 << 5)
#define SDI		(1 << 6)
#define SDO		(1 << 4)

// other defines here
#define SC SendChar

// user global variables
uint8_t data[6];


uint8_t i;

uint8_t val;
uint8_t led0 = 0;
uint8_t countVal = 0;

// sending char to UART function
static void SendChar(uint8_t data) {
	while (!(UART_STATUS & (1<<UART_TXREADY)));
	UART_DATA = data;
}

// sending integer to UART function

static void SendInt(uint32_t data) {
	uint8_t stroka[7];
	itoa(data, stroka, 10);
	SC(' ');
	for(i=0; i<=7; i++)
	{ 
	  //if((stroka[0] == 0)|(stroka[1])) i++;
	  SC(stroka[i]);
	  stroka[i] = 0;
	}
	SC(' ');
}


// UART receive interrupt
/*
ISR(USART_RXC_vect) {
	val = UART_DATA;
	SC(' ');SC('O');SC('K');SC(' ');
	
	if(val == '1') {
		TCCR2 |= (1 << CS22)|(1 << CS21)|(1 << CS20);
	}
	if(val == '2') {
		TCCR2 &= ~(1 << CS22)& ~(1 << CS21) & ~(1 << CS20);
	}
	if(val == '3') {
		TCCR2 &= ~(1 << CS22)& ~(1 << CS21) & ~(1 << CS20);
		TCCR2 |= (1 << CS21);
	}
    if(val == 'x') {
		PORTD ^= (1 << 3);
		SC(' ');SC('O');SC('K');SC(' ');
	}


}	
*/

// timer 0 overflow
/*
ISR(TIMER0_OVF_vect) {

}
*/

// timer 0 compare
/*
ISR(TIMER0_COMP_vect) {
  
  SC('!');

}
*/

// timer 2 overflow
/*
ISR(TIMER2_OVF_vect) {

}
*/

// timer 2 compare
/*
ISR(TIMER2_COMP_vect) {
	
}
*/

//external interrupt 0
/*
ISR(INT0_vect) {  

}
*/


int main(void) {
	/* ***********************************
	*               UART SET UP
	*************************************/
	UART_BAUD_HIGH = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF; //set baudrate
	UART_BAUD_LOW = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF);
	UART_CTRL = UART_CTRL_DATA; //set enable UART
	UART_CTRL2 = UART_CTRL2_DATA; //set byte and other features
	//UCSRB |= (1 << RXCIE); // enable interrupts
	
	/* **********************************
	 *               PORTS ASSIGNMENT
	 ***********************************/
	
	DDRB |= (1 << 2);
	DDRD |= (1 << 6);
	PORTD &= ~(1 << 6);
	DDRD |= (1 << 3);
	DDRD &= ~(1 << 4);
	PORTD &= ~(1 << 3);
	PORTD |= (1 << 4);
	
	DDRD |= (1 << 5);

//	SC(' '); //first terminal byte
	
	sei();
	
	/***********************************
	 *             THE MAIN LOOP
	 * ********************************/

	cli();
	TIMSK |= (1 << TOIE2)|(1 << OCIE2)|(1 << TOIE0); // overflow interrupt by timer 2  
	//TIMSK0 |= (1 << TOIE2);
	//GICR |= (1 << INT0);
	//MCUCR |= (1 << ISC00);
	
	OCR2 = 10; // output compare
	
	TCCR0 |= (1 << CS02)|(1 << CS01)|(1 << CS00);
	TCCR2 |= (1 << CS20);
	
	sei();
	while(1) {
		//PORTD ^= (1 << 5);
		//SendInt(countVal*11.5);
		led0++;
	}
	return 0;
}